#include "hotkey.h"

HotKey::HotKey(QWidget *parent) : QWidget(parent)
{
    InitUI();
}

void HotKey::InitUI()
{
    resize(400,300);
    tb_view = new QTableView(this);
    model = new QStandardItemModel(this);
    QHeaderView* head =  tb_view->horizontalHeader();
    head->setStretchLastSection(true);
    head->setResizeContentsPrecision(QHeaderView::Stretch);
    tb_view->setModel(model);
    tb_view->resize(400,300);
    set_key_list();
    setup_ui();
    show();
}

void HotKey::set_key_list()
{
    key_struct* tmp = new key_struct;
    tmp->key_str = "Ctrl + S";
    tmp->tip = "保存当前文件";
    key_list.append(tmp);
    tmp = new key_struct;
    tmp->key_str = "Ctrl + W";
    tmp->tip = "保存所有文件";
    key_list.append(tmp);
    tmp = new key_struct;
    tmp->key_str = "Ctrl + E";
    tmp->tip = "关闭当前文件";
    key_list.append(tmp);
    tmp = new key_struct;
    tmp->key_str = "Ctrl + Q";
    tmp->tip = "关闭所有文件";
    key_list.append(tmp);
    tmp = new key_struct;
    tmp->key_str = "F6";
    tmp->tip = "打开或关闭文件浏览器";
    key_list.append(tmp);
    tmp = new key_struct;
    tmp->key_str = "Ctrl + F";
    tmp->tip = "查找和替换";
    key_list.append(tmp);
}

void HotKey::setup_ui()
{
    QList<key_struct*>::iterator iter = key_list.begin();
    model->setColumnCount(2);
    model->setHeaderData(0, Qt::Horizontal, "快捷键");
    model->setHeaderData(1, Qt::Horizontal, "作用");
    int current_y = 0;
    int line_height = 30;
    int tip_width = 100;
    int key_str_width = 100;
    while(iter != key_list.end()){
        model->setItem(current_y, 0, new QStandardItem( (*iter)->key_str ));
        model->setItem(current_y, 1, new QStandardItem( (*iter)->tip ) );
        ++current_y;
        ++iter;
    }
}

void HotKey::resizeEvent(QResizeEvent *e)
{
    tb_view->resize(e->size().width(), e->size().height());
}

bool is_key_event_emit(QKeyEvent *event)
{
    if (event->modifiers() == (Qt::ControlModifier) && event->key() == Qt::Key_S){
        GlobalSignalMgr::get_instance()->notify(SignalType::SIG_FILE_SAVE_SINGLE_VOID);
        return false;
    }
    else if (event->modifiers() == (Qt::ControlModifier) && event->key() == Qt::Key_W){
        GlobalSignalMgr::get_instance()->notify(SignalType::SIG_FILE_SAVE_ALL_VOID);
        return false;
    }
    else if (event->modifiers() == (Qt::ControlModifier) && event->key() == Qt::Key_E){

        GlobalSignalMgr::get_instance()->notify(SignalType::SIG_FILE_CLOSE_ONE_VOID);
        return false;
    }
    else if (event->modifiers() == (Qt::ControlModifier) && event->key() == Qt::Key_Q){
        GlobalSignalMgr::get_instance()->notify(SignalType::SIG_FILE_CLOSE_ALL_VOID);
        return false;
    }
    else if (event->key() == Qt::Key_F6){
        GlobalSignalMgr::get_instance()->notify(SignalType::SIG_FILEBROWSER_SHOWORHID_VOID);
        return false;
    }
    else if (event->modifiers() == (Qt::ControlModifier) && event->key() == Qt::Key_F){
        GlobalSignalMgr::get_instance()->notify(SignalType::SIG_SEARCH_FORM_OPEN_VOID);
        return false;
    }
    return true;
}
